package ij.hkust.gui.laserCtr.STORM;

import ij.IJ;

import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class StormLaser extends JPanel{

	private static final long serialVersionUID = 1L;
	private String laser;//the laser ID
	public ArrayList<OneFrame> frames = new ArrayList<OneFrame>();
	private int startx = 70,starty = 8;
	private JRadioButton flagRadio = new JRadioButton();
	private OneFrame selectFrame;
	private PeriodEditor parent;
	private int periodTime;
	private JLabel laserLabel = new JLabel();
	
	public StormLaser(){
		super();
		this.setSize(444, 30);
		this.setLayout(null);
		this.setBackground(Color.LIGHT_GRAY);
		flagRadio.setBounds(6, 4, 21, 23);
		this.add(flagRadio);
		flagRadio.setBackground(this.getBackground());
		this.addFocusListener(focusAdapter);
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				clicked();
			}
		});
//		laserLabel.setLocation(33, 8);
		laserLabel.setForeground(Color.BLACK);
		laserLabel.setBounds(30, 4, 54, 20);
		this.add(laserLabel);
	}
	
	public void addFrame(){
//		if(frames.size() >=18){
//			IJ.error("Too many frames!");
//			return;
//		}
		OneFrame newFrame = new OneFrame();
		newFrame.setLocation(startx, starty);
		newFrame.setSize(20, 15);
		newFrame.setBackground(this.getBackground());//the same color as the 
		newFrame.addFocusListener(focusAdapter);
		newFrame.setParent(this);
		newFrame.setText("ON");
		newFrame.setTime(50);//the default time is 50
		frames.add(newFrame);
		startx += 20;
		this.add(newFrame);
	}

	public void addFrame(OneFrame frame){
		frame.setLocation(startx, starty);
		frame.setSize(20, 15);
		frame.setBackground(this.getBackground());//the same color as the laser
		frame.addFocusListener(focusAdapter);
		frame.setParent(this);
		frame.setText("ON");
		
		frames.add(frame);
		startx += 20;
		this.add(frame);
	}
	/**
	 * delete the frame from the panel
	 */
	public void delFrame(){
		if(selectFrame != null){
			frames.remove(selectFrame);
			this.remove(selectFrame);
			selectFrame = null;
			startx = 70;
			for(int i=0;i<frames.size();i++){
				frames.get(i).setLocation(startx, starty);
				startx += 20;
			}
		}
	}
	public void delFrame(int index){
		if(index>0 && index<frames.size()){
			OneFrame tmp = frames.get(index);
			if(tmp.equals(selectFrame)){
				selectFrame = null;
			}
			frames.remove(tmp);
			this.remove(tmp);
			startx = 70;
			for(int i=0;i<frames.size();i++){
				frames.get(i).setLocation(startx, starty);
				startx += 20;
			}
		}
	}
//
	public void setFrameData(int time,int activate,int intensity){
		if(selectFrame != null){
			selectFrame.setTime(time);
//			selectFrame.setActivate(activate);
			selectFrame.setIntensity(intensity);
		}
	}
//	
	public void setFrameTime(int time){
		if(selectFrame != null){
			selectFrame.setTime(time);
		}
	}
	public void setFrameAct(int activate){
		if(selectFrame != null){
//			selectFrame.setActivate(activate);
		}
	}
	public void setFrameInt(int intensity){
		if(selectFrame != null){
			selectFrame.setIntensity(intensity);
		}
	}

	public void getFocus(){
		flagRadio.setSelected(true);
	}
	public void lostFocus(){
		flagRadio.setSelected(false);
	}
	public void clicked(){
		parent.cleanSelect();
		flagRadio.setSelected(true);
		parent.setSelectLaser(this);
		if(selectFrame!=null){
			parent.refreshSlider(selectFrame.getIntensity(), selectFrame.getTime());
		}
	}
//	
	public void cleanColor(){
		for(int i=0;i<frames.size();i++){
			frames.get(i).setForeground(Color.black);
		}
	}
	
	
	FocusAdapter focusAdapter = new FocusAdapter() {
		@Override
		public void focusGained(FocusEvent arg0) {
			getFocus();
		}

		@Override
		public void focusLost(FocusEvent e) {
			lostFocus();
			JComponent tmp = (JComponent)e.getSource();
			tmp.setBackground(Color.LIGHT_GRAY);
		}
		
	};
//	
//	
//	
	public String getLaser() {
		return laser;
	}

	public void setLaser(String laser) {
		this.laser = laser;
		laserLabel.setText(laser);
	}

	public OneFrame getSelectFrame() {
		return selectFrame;
	}

	public void setSelectFrame(OneFrame selectFrame) {
		this.selectFrame = selectFrame;
	}

	public ArrayList<OneFrame> getFrames() {
		return frames;
	}

	public void setFrames(ArrayList<OneFrame> frames) {
		this.frames = frames;
	}
//
//	public PeriodEditor getParent() {
//		return parent;
//	}

	public void setParent(PeriodEditor parent) {
		this.parent = parent;
	}
//	

	public int getPeriodTime() {
		return periodTime;
	}

	public void setPeriodTime(int periodTime) {
		this.periodTime = periodTime;
	}
	
	public void clearAllFrame(){
		while(frames.size()>0){
			this.remove(frames.get(0));
			frames.remove(0);
		}
//		frames.clear();
		startx = 70;
	}
}
